// hyb_yl/zhuanjiaziliao/zhuanjiaziliao.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    radioIndex: 1,
    hide: true,
    touxiang: true,
    touxiangurl: "",
    upload_picture_list: [],
    uplogo: '',
    imgs: [],
    zhuce: 0,
    array1: [], //科室
    indexx: null,
    array: [], //科室
    yiyuan: [],
    ids: [],
    index: null,
    date: "",
    zhiwuindex: null, //新增
  },
  getKeshi: function() {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/Keshi',
      success: function(res) {
        that.setData({
          keshi: res.data.data.name,
          keshiid: res.data.data.id
        })
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },

  formsubmit: function(e) {
    var that = this
    //console.log(e)
    var id = that.data.id;
    var openid = wx.getStorageSync('openid');
    var values = e.detail.value
    var z_name = values.z_name
    var z_sex = values.z_sex
    var z_telephone = values.z_telephone
    var z_yiyuan = values.z_yiyuan
    var z_zhiwu = values.z_zhiwu
    var z_content = values.z_content
    var uplogo = that.data.uplogo;
    var pid = that.data.pid

    if (e.detail.value.z_name.length == 0 || e.detail.value.z_telephone.length == 0) {
      wx.showModal({
        content: '姓名和手机号不能为空',
      })
      return false;
    } else if (e.detail.value.z_yiyuan.length == 0) {
      wx.showModal({
        content: '请填写所在医院',
      })
      return false;
    } else if (e.detail.value.pid.length == 0) {
      wx.showModal({
        content: '请选择科室',
      })
      return false;
    } else if (e.detail.value.z_zhiwu.length == 0) {
      wx.showModal({
        content: '请填写真实职务',
      })
      return false;
    } else {
      wx.showModal({
        title: '提示',
        content: ' 确认提交么？ ',
        success: function(res) {
          if (res.confirm) {
            // console.log('用户点击确定');
            var that = this;
            var values = e.detail.value;
            app.util.request({
              url: 'entry/wxapp/zhuanjiash',
              data: {
                openid: openid,
              },
              header: {
                'Content-Type': 'application/json'
              },
              success: function(res) {
                var dataimg = res.data
                //console.log(dataimg)
                app.util.request({
                  url: 'entry/wxapp/Zimagesnew',
                  data: {
                    id: id,
                    z_name: z_name,
                    z_sex: z_sex,
                    z_telephone: z_telephone,
                    z_yiyuan: z_yiyuan,
                    z_zhiwu: z_zhiwu,
                    z_content: z_content,
                    openid: openid,
                    z_thumbs: uplogo,
                    z_room: pid
                  },
                  header: {
                    'Content-Type': 'application/json'
                  },
                  success: function(res) {
                    // console.log(res)
                    wx.showLoading({
                      title: "修改成功",
                    })
                    setTimeout(function() {
                      wx.hideLoading();
                      wx.redirectTo({
                        url: '../my/my',
                      })
                    }, 800)
                  },
                  fail: function(err) {
                    console.log(err);
                  }
                })

              },
              fail: function(err) {
                console.log(err);
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    }

  },
  deletetouxiang: function() {
    this.setData({
      touxiang: true,
      touxiangurl: ""
    })
  },
  touxiang: function() {
    var that = this;
    var uniacid = app.siteInfo.uniacid;
    var imgss = that.data.imgs;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        //console.log(tempFilePaths);
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploads&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {
            'path': 'wxchat'
          },
          success: function(res) {
            console.log(res.data)
            that.setData({
              uplogo: res.data,
              touxiangurl: tempFilePaths,
              touxiang: false
            })
          },
          fail: function(res) {
            console.log(res)
          },
        })
        that.setData({
          logo: tempFilePaths
        })
      }
    })
  },

  uploadpic: function(e) {
    var that = this
    var upload_picture_list = that.data.upload_picture_list
    wx.chooseImage({
      count: 8, // 默认9，这里显示一次选择相册的图片数量
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFiles = res.tempFiles
        //循环把图片加入上传列表
        for (var i in tempFiles) {
          tempFiles[i]['upload_percent'] = 0
          tempFiles[i]['path_server'] = ''
          upload_picture_list.push(tempFiles[i])
        }
        that.setData({
          upload_picture_list: upload_picture_list,
        })
        if (upload_picture_list.length == 3) {
          that.setData({
            hide: true
          })
        }
        //循环把图片上传到服务器 并显示进度
        for (var j in upload_picture_list) {
          if (upload_picture_list[j]['upload_percent'] == 0) {
            upload_file_server(that, upload_picture_list, j)
          }
        }
      }
    })

    function upload_file_server(that, upload_picture_list, j) {
      var uniacid = app.siteInfo.uniacid;
      //console.log(uniacid);
      var openid = wx.getStorageSync('openid');
      // console.log("开始上传" + j + "图片到服务器：")
      // console.log(upload_picture_list[j])
      var upload_task = wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=msg_send_imgs&m=hyb_yl', //需要用HTTPS，同时在微信公众平台后台添加服务器地址
        filePath: upload_picture_list[j]['path'], //上传的文件本地地址
        name: 'file',
        formData: {
          'path': 'wxchat',
          openid: openid,
          uniacid: uniacid,
          i_type: 2
        }, //附近数据，这里为路径
        success: function(res) {
          console.log(res)
          var data = res.data //字符串转化为JSON
          that.setData({
            thumb: data,
            upload_picture_list: upload_picture_list
          })
          console.log("图片上传" + j + "到服务器完成：")
          //console.log(upload_picture_list[j]['path'])
        }
      })
      upload_task.onProgressUpdate((res) => {
        // console.log('上传进度', res.progress)
        // console.log('已经上传的数据长度', res.totalBytesSent)
        // console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
        upload_picture_list[j]['upload_percent'] = res.progress
        console.log('第' + j + '个图片上传进度：' + upload_picture_list[j]['upload_percent'])
        console.log(upload_picture_list)
        that.setData({
          upload_picture_list: upload_picture_list
        })

      })
    }
  },
  deleteimg: function(e) {
    var i = e.currentTarget.dataset.index;
    var imgs = this.data.imgs;
    imgs.splice(i, 1);
    this.setData({
      imgs: imgs,
      hide: false
    })
  },
  radio: function(e) {
    this.setData({
      radioIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getKeshi: function() {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/Keshi',
      success: function(res) {
        that.setData({
          keshi: res.data.data.name,
          keshiid: res.data.data.id
        })
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    var id = options.id
    var uniacid = app.siteInfo.uniacid;
    var uniacid = app.siteInfo.uniacid;
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        //console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    app.util.request({
      'url': 'entry/wxapp/Zjinfo',
      'data': {
        id: id
      },
      success: function(res) {
        console.log(res)
        var str = res.data.data.z_thumb;
        var arr = str.split(";");

        arr.splice(arr.length - 1, 1);
        if (arr.length < 3) {
          that.setData({
            hide: false
          })
        }
        var id = res.data.data.z_room
        app.util.request({
          'url': 'entry/wxapp/Parentid',
          'data': {
            id: id,
          },
          success: function(res) {
            that.setData({
              name: res.data.data.name
            })
          }
        })
        //console.log(arr.length);
        that.setData({
          userinfo: res.data.data,
          touxiangurl: res.data.data.z_thumbs,
          upload_picture_list: arr
        })
      },
      fail: function(err) {
        console.log(err)
      },

    })

    app.util.request({
      'url': 'entry/wxapp/Scurl',
      'cachetime': '0',
      success: function(res) {
        // console.log(res)
        that.setData({
          scurl: res.data.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getKeshi()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //职务选择
  zhiwu: function(e) {
    var that = this
    var dex = e.detail.value;
    var getpid = that.data.getpid;
    // console.log(pid)
    var id = getpid[dex];
    //console.log(id)
    that.setData({
      pid: id,
      zhiwuindex: e.detail.value
    })
  },
  bindPickerChange1: function(e) {
    var that = this;
    var dex = e.detail.value;
    var ids = that.data.keshiid;
    var id = ids[dex];
    var arrs = that.data.zhiwu; //新增
    var zhiwuindex = ""
    app.util.request({
      'url': 'entry/wxapp/Category2',
      data: {
        id: id
      },
      success: function(res) {
        //console.log(res)
        that.setData({
          zhiwuss: res.data.data.name,
          getpid: res.data.data.id,
          zhiwuindex: zhiwuindex
        })
      },
      fail: function(err) {
        console.log(err)
      },

    })
    that.setData({
      index: e.detail.value,
      id: id,
      // zhiwuss: arr//新增
    })
  },
  bindPickerChange2: function(e) {
    var that = this;
    var dex = e.detail.value;
    var monery = that.data.monery;
    var pid = that.data.pid;
    var id = pid[dex];
    console.log(id);
    that.setData({
      indexx: dex,
      monerynum: monery[dex],
      id: id
    })
  },
})